<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="680px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改套餐' : '添加套餐'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="140px">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
         <el-form-item label="套餐名称:" prop="mealName">
           <el-input
             clearable
             :maxlength="20"
             :disabled="isUpdate"
             v-model="form.mealName"
             placeholder="请输入套餐名称"
           />
         </el-form-item>
           <el-form-item label="套餐有效时间/天:" prop="indate" class="delInput">
             <el-input
               clearable
               type="number"
               v-model.number="form.indate"
               placeholder="请输入套餐有效时间/天"
             />
           </el-form-item>
          <el-form-item label="建议零售价:" prop="retailPrice" class="delInput">
            <el-input
              type="number" step="0.01"
              clearable
              :maxlength="20"
              v-model="form.retailPrice"
              placeholder="请输入建议零售价"
            />
          </el-form-item>
          <el-form-item label="备注:" prop="remark">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.remark"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { emailReg, phoneReg } from 'ele-admin';
  import RoleSelect from './role-select.vue';
  import { mealAdd, mealUpdate, checkExistence } from '@/api/oem';
  import {JSEncrypt} from "jsencrypt";

  export default {
    components: { RoleSelect },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        types:2,
        id:'',
        mealName: '',
        retailPrice: '',
        indate: '',
        remark: '',
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          mealName: [
            {
              required: true,
              message: '请输入套餐名称',
              trigger: 'blur'
            }
          ],
          indate: [
            {
              required: true,
              message: '请输入有效期',
              trigger: 'blur'
            }
          ],
          retailPrice: [
            {
              required: true,
              message: '请输入零售价',
              trigger: 'blur'
            }
          ],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form,
          };
          const saveOrUpdate = this.isUpdate ? mealUpdate : mealAdd;
          if (!this.isUpdate) {
            let encryptor = new JSEncrypt()
            let pk = this.$store.state.signature.pk
            encryptor.setPublicKey(pk)
            data.password = encryptor.encrypt(data.password)
          }
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              let hint = this.isUpdate ? '修改套餐成功！' : '添加套餐成功！'
              this.$message.success(hint);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            delete this.form.types
            this.$util.assignObject(this.form, {
              ...this.data,
              password: null,
              sex: this.data.sex + ''
            });
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
<style scoped>
  /*隐藏input（type=number)右边的小箭头*/
  /deep/.delInput input::-webkit-outer-spin-button,
  /deep/.delInput input::-webkit-inner-spin-button {
    -webkit-appearance: none!important;
  }
  /deep/.delInput input[type="number"]{
    -moz-appearance: textfield;
  }
</style>

